Titles (ggplot2)

Problem

You want to set the title of your graph.

Solution

An example graph without a title:

library(ggplot2)
bp <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_boxplot()
bp

With a title:

bp + opts(title="Plant growth")

# If the title is long, it can be split into multiple lines with \n
bp + opts(title="Plant growth with\ndifferent treatments")

# Reduce line spacing and use bold text
bp + opts(title="Plant growth with\ndifferent treatments") + 
     opts(plot.title = theme_text(size=14, lineheight=.8, face="bold"))